package com.lonwin.inspectionmanagement.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.utils.DataHelper;
import com.lonwin.inspectionmanagement.R;
import com.lonwin.inspectionmanagement.R2;
import com.lonwin.inspectionmanagement.app.AppConstants;
import com.lonwin.inspectionmanagement.di.component.DaggerPatrolComponent;
import com.lonwin.inspectionmanagement.di.module.PatrolModule;
import com.lonwin.inspectionmanagement.mvp.contract.PatrolContract;
import com.lonwin.inspectionmanagement.mvp.model.api.Api;
import com.lonwin.inspectionmanagement.mvp.model.entity.bean.ConfigBean;
import com.lonwin.inspectionmanagement.mvp.model.entity.bean.CruiseLocusBean;
import com.lonwin.inspectionmanagement.mvp.model.entity.bean.FilterBean;
import com.lonwin.inspectionmanagement.mvp.model.entity.bean.HhkqLhAliasBean;
import com.lonwin.inspectionmanagement.mvp.model.entity.bean.RiverBean;
import com.lonwin.inspectionmanagement.mvp.presenter.PatrolPresenter;
import com.lonwin.inspectionmanagement.mvp.ui.adapter.RiverAdapter;
import com.lonwin.inspectionmanagement.mvp.ui.adapter.TextAdapter;
import com.lonwin.inspectionmanagement.widget.ArrowDownUpView;
import com.lonwin.inspectionmanagement.widget.RecylerPopwindow;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.jess.arms.utils.Preconditions.checkNotNull;


public class PatrolActivity extends BaseActivity<PatrolPresenter> implements PatrolContract.View, RecylerPopwindow.ShowListener {

    @BindView(R2.id.river)
    ArrowDownUpView river;
    @BindView(R2.id.year)
    ArrowDownUpView year;
    @BindView(R2.id.rvcontent)
    RecyclerView rvcontent;
    @BindView((R2.id.ll_choice))
    LinearLayout ll_choice;
    @BindView(R2.id.vcover)
    View vcover;
    private RiverAdapter riverAdapter;
    private List<HhkqLhAliasBean.DataBean> mdataBeans;//所有河湖信息
    private int page = 1;
    private int selectIndex = 0;
    private ConfigBean configBean;
    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerPatrolComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .patrolModule(new PatrolModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_patrol; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
         configBean = DataHelper.getDeviceData(this, AppConstants.EXTRA_CONFIG_DATA);
        mdataBeans = (List<HhkqLhAliasBean.DataBean>) getIntent().getSerializableExtra(AppConstants.EXTRA_DATA);
        if (mdataBeans != null) {
            intRiver();
        }
        initYear();
        initRvContent();
    }

    private void initRvContent() {
//        View headerView = LayoutInflater.from(this).inflate(R.layout.river_header,null,false);
        List<CruiseLocusBean.DataBean> dataBeans = new ArrayList<>();
        riverAdapter = new RiverAdapter(dataBeans);
        riverAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_RIGHT);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        ArmsUtils.configRecyclerView(rvcontent, linearLayoutManager);
        rvcontent.setAdapter(riverAdapter);
        riverAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.cruise_recording_item_point_btn) {
                    if (riverAdapter.getItem(position).getQuestcount()<1)
                    {
                        showMessage("没有打点记录");
                        return;
                    }
                    Intent intent = new Intent(PatrolActivity.this, PointRecordingActivity.class);
                    intent.putExtra(AppConstants.EXTRA_DATA, (Serializable) mdataBeans);
                    intent.putExtra("xcid",riverAdapter.getItem(position).getId());
                    startActivity(intent);
                } else {
                    Intent intent = new Intent(PatrolActivity.this, CruiseLocusActivity.class);
                    intent.putExtra("xcid",riverAdapter.getItem(position).getId());
                    startActivity(intent);
                }

            }
        });
        riverAdapter.disableLoadMoreIfNotFullPage(rvcontent);
        riverAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                mPresenter.queryCruiseHistory(configBean.getAdcd() ,"2018", String.valueOf(page), selectIndex == 0 ? null : String.valueOf(mdataBeans.get(selectIndex).getId().getGroupID()), false);
            }
        });
        mPresenter.queryCruiseHistory(configBean.getAdcd(), "2018", String.valueOf(page), selectIndex == 0 ? null : String.valueOf(mdataBeans.get(selectIndex).getId().getGroupID()), false);

    }

    private void intRiver() {
        TextAdapter textAdapter = new TextAdapter(mdataBeans);
        RecylerPopwindow<TextAdapter> recylerPopwindow = new RecylerPopwindow<TextAdapter>(textAdapter, this);
        recylerPopwindow.setShowListener(this);
        textAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                for (FilterBean item : mdataBeans) {
                    item.setSelected(false);
                }
                mdataBeans.get(position).setSelected(true);
                selectIndex = position;
                textAdapter.notifyDataSetChanged();
                if (recylerPopwindow.isShowing()) {
                    recylerPopwindow.dismiss();
                }
                river.updateTitle(mdataBeans.get(position).getTitle());
                mPresenter.queryCruiseHistory(configBean.getAdcd(), "2018", String.valueOf(page), selectIndex == 0 ? null : String.valueOf(mdataBeans.get(selectIndex).getId().getGroupID()), false);

            }
        });
        recylerPopwindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                vcover.setVisibility(View.GONE);
                river.rotate180Arrow();
            }
        });
        river.setExpandListener(new ArrowDownUpView.ExpandListener() {
            @Override
            public void expand(View view) {
                recylerPopwindow.showAsDropDown(ll_choice);
            }

            @Override
            public void close(View view) {
                recylerPopwindow.dismiss();
            }
        });
    }

    private void initYear() {
        List<FilterBean> yeardatas = new ArrayList<>();
        yeardatas.add(new FilterBean("2018年1月"));
        yeardatas.add(new FilterBean("2018年2月"));
        yeardatas.add(new FilterBean("2018年3月"));
        yeardatas.add(new FilterBean("2018年4月"));
        yeardatas.add(new FilterBean("2018年5月"));
        yeardatas.add(new FilterBean("2018年6月"));
        yeardatas.add(new FilterBean("2018年7月"));
        yeardatas.add(new FilterBean("2018年8月"));
        TextAdapter yearAdapter = new TextAdapter(yeardatas);
        RecylerPopwindow<TextAdapter> yearPopwindow = new RecylerPopwindow<TextAdapter>(yearAdapter, this);
        yearPopwindow.setShowListener(this);
        yearPopwindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                vcover.setVisibility(View.GONE);
                year.rotate180Arrow();
            }
        });
        yearAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                for (FilterBean item : yeardatas) {
                    item.setSelected(false);
                }
                yeardatas.get(position).setSelected(true);
                yearAdapter.notifyDataSetChanged();
                if (yearPopwindow.isShowing()) {
                    yearPopwindow.dismiss();
                }
                year.updateTitle(yeardatas.get(position).getTitle());
                mPresenter.queryCruiseHistory(configBean.getAdcd(), "2018", String.valueOf(page), selectIndex == 0 ? null : String.valueOf(mdataBeans.get(selectIndex).getId().getGroupID()), false);
            }
        });
        year.setExpandListener(new ArrowDownUpView.ExpandListener() {
            @Override
            public void expand(View view) {
                yearPopwindow.showAsDropDown(ll_choice);
            }

            @Override
            public void close(View view) {
                yearPopwindow.dismiss();
            }
        });
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.makeText(this,message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public void show() {
        vcover.setVisibility(View.VISIBLE);
    }

    @Override
    public void queryCruiseHistorySuccess(List<CruiseLocusBean.DataBean> dataBeans, boolean isFersh) {
        if (dataBeans == null || dataBeans.isEmpty()) {
            riverAdapter.loadMoreEnd();
            ArmsUtils.makeText(this, "没有记录信息");
            if (page == 1) {
                riverAdapter.setEmptyView(ArmsUtils.inflate(this, R.layout.empty_view));
            }
            return;
        }
        riverAdapter.loadMoreComplete();
        page++;
        if (isFersh) {
            riverAdapter.setNewData(dataBeans);
            return;
        }
        riverAdapter.getData().addAll(dataBeans);
        riverAdapter.notifyDataSetChanged();
//        riverAdapter.addData(dataBeans);
    }
}
